# -*- coding: utf-8 -*-
"""
Created on Thu May 18 09:45:00 2017

@author: WANGWEIW23
"""

from painting_area import *
from indicator import *
import matplotlib.pyplot as plt
import os
import sys
import numpy
import math
import hashlib
import matplotlib.image as mping
import re
import numpy as np
import pandas as pd
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
#读取文件夹下面所有的pts文件
def read_data(path='D:/temp'):
    pattern = re.compile(r'([X\d]{18})')
    pathDir_M = os.listdir(path)
    pathDir_F=[]
    for files in pathDir_M:
        pathDir_F.append(os.path.splitext(files)[0])
    fileList = [os.path.join(path, f) for f in os.listdir(path) if f.startswith('.')==False]
    pos_data = []
    idno = []
    for filename in fileList:
        f = open(filename,'r')
        count = 0
        landmark = np.zeros((68,2))
        for line in f.readlines(): 
            if count>=3 and count<=70:
                x,y = line.split(' ')
                landmark[count-3,0] = float(x)
                landmark[count-3,1] = float(y)
            count += 1
        pos_data.append(landmark)
        m = pattern.search(filename)
        try:
            idno.append(md5(m.group(1)))
        except:
            print(filename)
        f.close()
    return pos_data,pathDir_F,fileList

      
#单个文件分析
def single_file_statis(file1,picfile):
        #os.chdir("D:\\Users\\WANGWEIW23\\.spyder-py3")
        test=open(file1)
        a=coordinate(test)
        point1=computer_mean_point(a[3:10])  ###计算对应的一个区域的中点数值
        point2=a[9]
        point3=a[11]
        point4=a[13]
        point5=computer_mean_point(a[13:20])  ####计算对应相应区域中点数值
        point6=computer_mean_point(a[51:71])
        point7=computer_mean_point(a[30:34])
        point8=computer_mean_point(a[20:25])
        point9=computer_mean_point(a[25:30])
#        left_abc=poly_forming([point1,point2,point6,point7,point8])
#        right_abc=poly_forming([point4,point5,point9,point7,point6])
#        folling_abc=poly_forming([point2,point3,point4,point6])
#        above_abc=poly_forming([point7,point8,point9])
#        left_area_point=area_point(5,left_abc)
#        right_area_point=area_point(5,right_abc)
#        folling_area_point=area_point(4,folling_abc)
#        above_area_point=area_point(3,above_abc)
        ####左边区域的多边形
        left_abc=[point1,point2,point6,point7,point8]
        ####右边区域的多边形顶点
        right_abc=[point4,point5,point9,point7,point6]
        folling_abc=[point2,point3,point4,point6]
        above_abc=[point7,point8,point9]
        ####左边区域的所有像素点数值的索引，下面依次类推
        left_area_point=area_point(5,left_abc)
        right_area_point=area_point(5,right_abc)
        folling_area_point=area_point(4,folling_abc)
        above_area_point=area_point(3,above_abc) 
        ####计算对应左边区域点的数值，下面依次类推           
        [YIQ_statis,YCbCr_statis]=correspond_indicator(left_area_point,picfile)        
        [YIQ_statis_right,YCbCr_statis_right]=correspond_indicator(right_area_point,picfile)
        [YIQ_statis_above,YCbCr_statis_above]=correspond_indicator(above_area_point,picfile)
        [YIQ_statis_folling,YCbCr_statis_folling]=correspond_indicator(folling_area_point,picfile)
        return YIQ_statis,YCbCr_statis,YIQ_statis_right,YCbCr_statis_right,YIQ_statis_above,YCbCr_statis_above,YIQ_statis_folling,YCbCr_statis_folling


###计算pts文件对应的图片文件的索引找出来(根据前面的字符串对应)  
def img_corres2(pts):
    if pathDir_pic2.index(pts[0:18]):
       global idx
       idx=pathDir_pic2.index(pts[0:18])
    return idx

def img_corres1(pts):
    if pathDir_pic1.index(pts[0:18]):
       global idx
       idx=pathDir_pic1.index(pts[0:18])
    return idx

###将指标数据统计成dataframe的格式
def to_csv(YIQ_total):
    ID_list=[]
    Y_mean=[]
    Y_std=[]
    Y_min=[]
    Y_max=[]
    Y_mean_right=[]
    Y_std_right=[]
    Y_min_right=[]
    Y_max_right=[]
    Y_mean_above=[]
    Y_std_above=[]
    Y_min_above=[]
    Y_max_above=[]
    Y_mean_folling=[]
    Y_std_folling=[]
    Y_min_folling=[]
    Y_max_folling=[]
    I_mean=[]
    I_std=[]
    I_min=[]
    I_max=[]
    I_mean_right=[]
    I_std_right=[]
    I_min_right=[]
    I_max_right=[]    
    I_mean_above=[]
    I_std_above=[]
    I_min_above=[]
    I_max_above=[]
    I_mean_folling=[]
    I_std_folling=[]
    I_min_folling=[]
    I_max_folling=[]
    Q_mean=[]
    Q_std=[]
    Q_min=[]
    Q_max=[]
    Q_mean_right=[]
    Q_std_right=[]
    Q_min_right=[]
    Q_max_right=[]
    Q_mean_above=[]
    Q_std_above=[]
    Q_min_above=[]
    Q_max_above=[]
    Q_mean_folling=[]
    Q_std_folling=[]
    Q_min_folling=[]
    Q_max_folling=[]
    #一直到了第250个文件之后I_min_folling.append(YIQ_total[j+4][6]),IndexError: list index out of range    
    for j in range(0,len(YIQ_total),5):
      try:
         ID_list.append(md5(str(YIQ_total[j])))
         Y_mean.append(YIQ_total[j+1][0])
         Y_std.append(YIQ_total[j+1][1])
         Y_min.append(YIQ_total[j+1][2])
         Y_max.append(YIQ_total[j+1][3])
         Y_mean_right.append(YIQ_total[j+2][0])
         Y_std_right.append(YIQ_total[j+2][1])
         Y_min_right.append(YIQ_total[j+2][2])
         Y_max_right.append(YIQ_total[j+2][3])
         Y_mean_above.append(YIQ_total[j+3][0])
         Y_std_above.append(YIQ_total[j+3][1])
         Y_min_above.append(YIQ_total[j+3][2])
         Y_max_above.append(YIQ_total[j+3][3])
         Y_mean_folling.append(YIQ_total[j+4][0])
         Y_std_folling.append(YIQ_total[j+4][1])
         Y_min_folling.append(YIQ_total[j+4][2])
         Y_max_folling.append(YIQ_total[j+4][3])
         I_mean.append(YIQ_total[j+1][4])
         I_std.append(YIQ_total[j+1][5])
         I_min.append(YIQ_total[j+1][6])
         I_max.append(YIQ_total[j+1][7])
         I_mean_right.append(YIQ_total[j+2][4])
         I_std_right.append(YIQ_total[j+2][5])
         I_min_right.append(YIQ_total[j+2][6])
         I_max_right.append(YIQ_total[j+2][7])
         I_mean_above.append(YIQ_total[j+3][4])
         I_std_above.append(YIQ_total[j+3][5])
         I_min_above.append(YIQ_total[j+3][6])
         I_max_above.append(YIQ_total[j+3][7])
         I_mean_folling.append(YIQ_total[j+4][4])
         I_std_folling.append(YIQ_total[j+4][5])
         I_min_folling.append(YIQ_total[j+4][6])
         I_max_folling.append(YIQ_total[j+4][7])
         Q_mean.append(YIQ_total[j+1][8])
         Q_std.append(YIQ_total[j+1][9])
         Q_min.append(YIQ_total[j+1][10])
         Q_max.append(YIQ_total[j+1][11])
         Q_mean_right.append(YIQ_total[j+2][8])
         Q_std_right.append(YIQ_total[j+2][9])
         Q_min_right.append(YIQ_total[j+2][10])
         Q_max_right.append(YIQ_total[j+2][11])
         Q_mean_above.append(YIQ_total[j+3][8])
         Q_std_above.append(YIQ_total[j+3][9])
         Q_min_above.append(YIQ_total[j+3][10])
         Q_max_above.append(YIQ_total[j+3][11])
         Q_mean_folling.append(YIQ_total[j+4][8])
         Q_std_folling.append(YIQ_total[j+4][9])
         Q_min_folling.append(YIQ_total[j+4][10])
         Q_max_folling.append(YIQ_total[j+4][11]) 
      except IndexError:
         pass
    a=[ID_list,Y_mean,Y_std,Y_min,Y_max,Y_mean_right,Y_std_right,Y_min_right,Y_max_right,
        Y_mean_above,Y_std_above,Y_min_above,Y_max_above,
        Y_mean_folling,Y_std_folling,Y_min_folling,Y_max_folling,
        I_mean,I_std,I_min,I_max,I_mean_right,I_std_right,I_min_right,I_max_right,
        I_mean_above,I_std_above,I_min_above,I_max_above,
        I_mean_folling,I_std_folling,I_min_folling,I_max_folling,
        Q_mean,Q_std,Q_min,Q_max,Q_mean_right,Q_std_right,Q_min_right,Q_max_right,
        Q_mean_above,Q_std_above,Q_min_above,Q_max_above,
        Q_mean_folling,Q_std_folling,Q_min_folling,Q_max_folling]
    df=pd.DataFrame(a,columns=a[0])
    df=df.T
    df.columns=['ID_list','Y_mean','Y_std','Y_min','Y_max','Y_mean_right','Y_std_right','Y_min_right','Y_max_right',
        'Y_mean_above','Y_std_above','Y_min_above','Y_max_above',
        'Y_mean_folling','Y_std_folling','Y_min_folling','Y_max_folling',
        'I_mean','I_std','I_min','I_max','I_mean_right','I_std_right','I_min_right','I_max_right',
        'I_mean_above','I_std_above','I_min_above','I_max_above',
        'I_mean_folling','I_std_folling','I_min_folling','I_max_folling',
        'Q_mean','Q_std','Q_min','Q_max','Q_mean_right','Q_std_right','Q_min_right','Q_max_right',
        'Q_mean_above','Q_std_above','Q_min_above','Q_max_above',
        'Q_mean_folling','Q_std_folling','Q_min_folling','Q_max_folling']
    return df


def to_csv1(YCbCr_total):
    ID_list=[]
    Cb_mean=[]
    Cb_std=[]
    Cb_min=[]
    Cb_max=[]
    Cb_mean_right=[]
    Cb_std_right=[]
    Cb_min_right=[]
    Cb_max_right=[]    
    Cb_mean_above=[]
    Cb_std_above=[]
    Cb_min_above=[]
    Cb_max_above=[]
    Cb_mean_folling=[]
    Cb_std_folling=[]
    Cb_min_folling=[]
    Cb_max_folling=[]
    Cr_mean=[]
    Cr_std=[]
    Cr_min=[]
    Cr_max=[]
    Cr_mean_right=[]
    Cr_std_right=[]
    Cr_min_right=[]
    Cr_max_right=[]
    Cr_mean_above=[]
    Cr_std_above=[]
    Cr_min_above=[]
    Cr_max_above=[]
    Cr_mean_folling=[]
    Cr_std_folling=[]
    Cr_min_folling=[]
    Cr_max_folling=[]
    #一直到了第250个文件之后I_min_folling.append(YIQ_total[j+4][6]),IndexError: list index out of range    
    for j in range(0,len(YCbCr_total),5):
      try:
         ID_list.append(md5(str(YCbCr_total[j])))
         Cb_mean.append(YCbCr_total[j+1][4])
         Cb_std.append(YCbCr_total[j+1][5])
         Cb_min.append(YCbCr_total[j+1][6])
         Cb_max.append(YCbCr_total[j+1][7])
         Cb_mean_right.append(YCbCr_total[j+2][4])
         Cb_std_right.append(YCbCr_total[j+2][5])
         Cb_min_right.append(YCbCr_total[j+2][6])
         Cb_max_right.append(YCbCr_total[j+2][7])
         Cb_mean_above.append(YCbCr_total[j+3][4])
         Cb_std_above.append(YCbCr_total[j+3][5])
         Cb_min_above.append(YCbCr_total[j+3][6])
         Cb_max_above.append(YCbCr_total[j+3][7])
         Cb_mean_folling.append(YCbCr_total[j+4][4])
         Cb_std_folling.append(YCbCr_total[j+4][5])
         Cb_min_folling.append(YCbCr_total[j+4][6])
         Cb_max_folling.append(YCbCr_total[j+4][7])
         Cr_mean.append(YCbCr_total[j+1][8])
         Cr_std.append(YCbCr_total[j+1][9])
         Cr_min.append(YCbCr_total[j+1][10])
         Cr_max.append(YCbCr_total[j+1][11])
         Cr_mean_right.append(YCbCr_total[j+2][8])
         Cr_std_right.append(YCbCr_total[j+2][9])
         Cr_min_right.append(YCbCr_total[j+2][10])
         Cr_max_right.append(YCbCr_total[j+2][11])
         Cr_mean_above.append(YCbCr_total[j+3][8])
         Cr_std_above.append(YCbCr_total[j+3][9])
         Cr_min_above.append(YCbCr_total[j+3][10])
         Cr_max_above.append(YCbCr_total[j+3][11])
         Cr_mean_folling.append(YCbCr_total[j+4][8])
         Cr_std_folling.append(YCbCr_total[j+4][9])
         Cr_min_folling.append(YCbCr_total[j+4][10])
         Cr_max_folling.append(YCbCr_total[j+4][11]) 
      except IndexError:
         pass
    a=[ID_list,
        Cb_mean,Cb_std,Cb_min,Cb_max,Cb_mean_right,Cb_std_right,Cb_min_right,Cb_max_right,
        Cb_mean_above,Cb_std_above,Cb_min_above,Cb_max_above,
        Cb_mean_folling,Cb_std_folling,Cb_min_folling,Cb_max_folling,
        Cr_mean,Cr_std,Cr_min,Cr_max,Cr_mean_right,Cr_std_right,Cr_min_right,Cr_max_right,
        Cr_mean_above,Cr_std_above,Cr_min_above,Cr_max_above,
        Cr_mean_folling,Cr_std_folling,Cr_min_folling,Cr_max_folling]
    df=pd.DataFrame(a,columns=a[0])
    df=df.T
    df.columns=['ID_list',
        'Cb_mean','Cb_std','Cb_min','Cb_max','Cb_mean_right','Cb_std_right','Cb_min_right','Cb_max_right',
        'Cb_mean_above','Cb_std_above','Cb_min_above','Cb_max_above',
        'Cb_mean_folling','Cb_std_folling','Cb_min_folling','Cb_max_folling',
        'Cr_mean','Cr_std','Cr_min','Cr_max','Cr_mean_right','Cr_std_right','Cr_min_right','Cr_max_right',
        'Cr_mean_above','Cr_std_above','Cr_min_above','Cr_max_above',
        'Cr_mean_folling','Cr_std_folling','Cr_min_folling','Cr_max_folling']
    return df


####程序的主入口    
if __name__ == '__main__':
    ###pts文件夹里面文件的读取
    pos_data,pathDir_F,fileList=read_data(path='D:/temp')
    ###图片文件夹里面文件的读取，这里显示的是有网格的数据分析结果
    filelist2,pathDir_pic2=eachFile("D:\\output\\grid")
    YIQ_total=[]
    YCbCr_total=[]
    ##一直到了1476个文件，之后后面就出错了， dim=rgb.shape[2],IndexError: tuple index out of range
    ###循环pts文件夹中的文件，输出所有的指标
    for i in range(len(pathDir_F)):
        try:
            idx=img_corres2(pathDir_F[i])
            YIQ_total.append(pathDir_pic2[idx])
            YCbCr_total.append(pathDir_pic2[idx])
            [YIQ_statis,YCbCr_statis,YIQ_statis_right,YCbCr_statis_right,YIQ_statis_above,YCbCr_statis_above,YIQ_statis_folling,YCbCr_statis_folling]=single_file_statis(fileList[i],filelist2[idx]);
            print(i)
            YIQ_total.append(YIQ_statis)
            YIQ_total.append(YIQ_statis_right)
            YIQ_total.append(YIQ_statis_above)
            YIQ_total.append(YIQ_statis_folling)
            YCbCr_total.append(YCbCr_statis)
            YCbCr_total.append(YCbCr_statis_right)
            YCbCr_total.append(YCbCr_statis_above)
            YCbCr_total.append(YCbCr_statis_folling)
        except (ValueError,IndexError):
            pass
    ###将YIQ指标和YCbCr指标数据转成dataframe的格式
    df1=to_csv(YIQ_total)
    df2=to_csv1(YCbCr_total)
    ###连接两个指标体系的指标的dataframe结构
    result=pd.concat([df1,df2],axis=1,join='inner')
    
    
####非网格的数据分析结果
    filelist1,pathDir_pic1=eachFile("D:\\output\\no-grid")
    YIQ_total1=[]
    YCbCr_total1=[]
    for i in range(len(pathDir_F)):
        try:
            idx=img_corres1(pathDir_F[i])
            YIQ_total1.append(pathDir_pic1[idx])
            YCbCr_total1.append(pathDir_pic1[idx])
            [YIQ_statis1,YCbCr_statis1,YIQ_statis_right1,YCbCr_statis_right1,YIQ_statis_above1,YCbCr_statis_above1,YIQ_statis_folling1,YCbCr_statis_folling1]=single_file_statis(fileList[i],filelist1[idx]);
            print(i)
            YIQ_total1.append(YIQ_statis1)
            YIQ_total1.append(YIQ_statis_right1)
            YIQ_total1.append(YIQ_statis_above1)
            YIQ_total1.append(YIQ_statis_folling1)
            YCbCr_total1.append(YCbCr_statis1)
            YCbCr_total1.append(YCbCr_statis_right1)
            YCbCr_total1.append(YIQ_statis_above1)
            YCbCr_total1.append(YIQ_statis_folling1)
        except (IndexError,ValueError):
            pass
    df11=to_csv(YIQ_total1)
    df12=to_csv1(YCbCr_total1)
    result1=pd.concat([df11,df12],axis=1,join='inner')



#####做出有无网格的图片集合的左脸Y-mean指标各自的historim并且计算kl离散度
result=result.dropna()
result1=result1.dropna()

###针对有网格和没有网格的图形分别做出指标I_mean直方图,然后其他指标相对其的离散度
result.to_csv('grid.csv')
num_bins = 50
plt.subplot(2,1,1)
plt.hist(result['I_mean'], num_bins, normed=1, facecolor='blue', alpha=0.5)
#KL=scipy.stats.entropy(list(result['Cb_mean']), list(result['I_mean']))
plt.title(r'$grid=I_mean$, $\sigma=15$')
plt.savefig('grid.png')

result1.to_csv('no-grid.csv')
plt.subplot(2,1,2)
plt.hist(result1['I_mean'], num_bins, normed=1, facecolor='blue', alpha=0.5)
plt.title(r'$no-grid=I_mean$, $\sigma=15$')
plt.savefig('no-grid.png')
plt.show()

#KL1 = scipy.stats.entropy(list(result['Y_mean']), list(result['I_mean']))
##plt.subplot(2,1,2)
#plt.hist(result1['I_mean'], num_bins, normed=1, facecolor='blue', alpha=0.5)
#   


    
    
